<?php
namespace app\data\controller;
use think\Db;
use \think\Request; 
class Data
{
    public function login(Request $request)
    {
        if($request->isPost()){
            $data= $request->post();
            $data['cookie']=$data['username'];
            $list = Db::name("administrator")->where($data)->find();
            if($list){
                echo 'OK';
            }else{
            echo 'error';
            }            
        }        
    }
    public function delete(Request $request)
    {
        if($request->isPost()){
             $data= $request->post();
             $id=$data['id'];
             Db::name('ad')->where('住院号', $id)->delete();
             Db::name('ad2')->where('住院号', $id)->delete();
             Db::name('adgrp')->where('住院号', $id)->delete();
        }
        else{
            echo 'error';
        }
    }
    
    public function download(Request $request)
    {
        if($request->isPost()){
            $data= $request->post();
            $id=$data['id'];
            shell_exec('D:\anaconda\python.exe D:\img\imget.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt "'.$id.'"' );
            $ba='.png';
            $link='http://localhost:8077/tp5.1test/public/data/Data/download2?link=D:/img/g';
            $ret=$link.$id.$ba;
            echo $ret;
        }
    }
    
    public function download2($link)
    {
            ob_clean ();
            header("Content-Type: image/png");
            $im = imagecreatefrompng ($link);
            imagepng ( $im );
            ob_end_flush();
            imagedestroy ( $im );
    }
    
    
    
    public function register(Request $request)
    {
        if($request->isPost()){
            $data= $request->post();
            $data['cookie']=$data['username'];
            Db::name('administrator')->insert($data);
            return 'OK';
        }
        else{
            echo 'error';
        }
    }    
    public function getkey(Request $request)
    {
        if($request->isPost()){
            $data= $request->post();
            #$key1=$data['关键字'];
            $key2=$data['key1'];
            $symbol=$data['Symbol'];
            $value=floatval($data['information']);
            if($key2!=null)
            {
                $res=Db::name('ad2')->field('住院号')->where($key2,$symbol,$value)->buildSql(true);
                $ret=Db::name('ad')->where('住院号','exp','IN '.$res)->select();
                return json($ret);
            }
            else
            {
                $data=Db::table('ad')->select();
                return json($data);
            }
        }
        else{
            echo 'error';
        }
    }         
    
    
    public function add(Request $request)
    {
        $data= $request->post();
        #$data=$data1['data'];
        #$data['性别']=$data1['Gender'];
        //json_encode($data,true);
        if($request->isPost()){
            #dump($data1);
            #Db::name('ad')->insert($data);
            #header("Content-type:text/html;charset=utf-8");
            $id=$data['住院号'];
            $blood=$data['血常规'];
            $bic=$data['生化'];
            $sht=$data['大小便'];
            $nblood=$data['凝血功能'];
            $ige=$data['免疫'];
            $mi=$data['分泌物培养结果'];
            #$order="D:\Python37_64\python.exe D:\op\operation.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt";
            $c = shell_exec('D:\anaconda\python.exe D:\op\operation.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt "'.$id.'" "'.$blood.'" "'.$bic.'" "'.$sht.'" "'.$nblood.'" "'.$ige.'" "'.$mi.'"' );
            #$c = shell_exec('D:\anaconda\python.exe D:\op\operation.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt ');
            $data['治疗方案']= $c;
            $res=Db::name('ad')->insert($data);
            #$data =  json_decode($data);
            #$url = "http://129.211.122.175/data/Data/add";
            #$res = $this->http_request($url, $data);
            echo $id;
        }
        else { dump('error?');}
    }
    public function modify(Request $request)
    {
        $data= $request->post();
        #$data=$data1['data'];
        #$data['性别']=$data1['Gender'];
        //json_encode($data,true);
        if($request->isPost()){
            #dump($data1);
            #Db::name('ad')->insert($data);
            #header("Content-type:text/html;charset=utf-8");
            $id=$data['住院号'];
            $blood=$data['血常规'];
            $bic=$data['生化'];
            $sht=$data['大小便'];
            $nblood=$data['凝血功能'];
            $ige=$data['免疫'];
            $mi=$data['分泌物培养结果'];
            Db::name('ad2')->where('住院号','=' , $id)->delete();
            Db::name('ad')->where('住院号','=' , $id)->delete();
            #$order="D:\Python37_64\python.exe D:\op\operation.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt";
            $c = shell_exec('D:\anaconda\python.exe D:\op\operation.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt "'.$id.'" "'.$blood.'" "'.$bic.'" "'.$sht.'" "'.$nblood.'" "'.$ige.'" "'.$mi.'"' );
            #$c = shell_exec('D:\anaconda\python.exe D:\op\modify.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt');
            #shell_exec('D:\anaconda\python.exe D:\img\imget.py 2>C:\Users\滑稽鸭阁\Desktop\LOG1.txt "'.$id.'"' );
            $data['治疗方案']= $c;
            $res=Db::name('ad')->insert($data);
            #$data =  json_decode($data);
            #$url = "http://129.211.122.175/data/Data/add";
            #$res = $this->http_request($url, $data);
            echo $id;
        }
        else { dump('error?');}
    }    
    
    function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (! empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    
    public function getall(Request $request)
    {
        if($request->isGET()){
            $data=Db::table('ad')->select();
            //$this->assign('datalist',json_encode($data));
            //$this->display();
            //$data['datalist']=$datalist;
            return json($data);
            //$this->ajaxRetrun($data,'JSON');
            
        }
    }   

    public function delete_id($chart,$id,$id_value)
    {
        Db::name($chart)->where($id, $id_value)->delete();
    }
}
